//
//  PKSMSUtils_Mob.swift
//  Panku
//
//  Created by mac on 2017/2/8.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import ReactiveCocoa
import ReactiveSwift
import Result

class PKSMSUtils_Mob: PKSMSUtils {
    
    static func make() -> PKSMSUtils {
        return PKSMSUtils_Mob()
    }
    
    private let customIdentifier = PKConfigServices.SMSSDK_customIdentifier()
    
    //发送验证码
    func commitVerificationCode(code: String!, phoneNumber: String!, zone: String!) -> SignalProducer<String, NSError> {
        
        let signalProducer = SignalProducer<String,NSError> { (observer, disposale) in
            SMSSDK.commitVerificationCode(code, phoneNumber: phoneNumber, zone: zone) { (_, error) in
                
                if error == nil{
                    observer.sendCompleted()
                }else{
                    observer.send(error: error as! NSError)

                }
            }

        }
        
        return signalProducer
    }

    //提交验证码
    func getVerificationCode(by: PKSMSGetCodeMethod, phoneNumber: String!, zone: String!) -> SignalProducer<String, NSError> {
        let signalProducer = SignalProducer<String,NSError> { (observer, disposale) in
            var method:SMSGetCodeMethod?
            
            if by == .sms{
                method = SMSGetCodeMethodSMS
            }else{
                method = SMSGetCodeMethodVoice
            }
            
            SMSSDK.getVerificationCode(by: method!, phoneNumber: phoneNumber, zone: zone, customIdentifier: self.customIdentifier) { (error) in
                
                if error == nil{
                    observer.sendCompleted()
                }else{
                    observer.send(error: error as! NSError)
                }
            }

            
        }
        
        return signalProducer
    }
    
    
}
